package cn.learn.webclient;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;

import java.text.MessageFormat;
import java.util.Map;

/**
 * @author huangyezhan
 * @version 1.0
 * @date 2020年03月23日18:42
 */
@RestController
public class WebClientDemo {

    /**
     * produces设置返回的格式
     * produces = MediaType.TEXT_EVENT_STREAM_VALUE
     *
     * @return
     */
    @RequestMapping(value = "webClient", method = RequestMethod.GET)
    public Flux<Map> findAll() {
        long      start = System.currentTimeMillis();
        Flux<Map> flux  = WebClient.create("localhost:8013/webClient").get().retrieve().bodyToFlux(Map.class);
        System.out.println(System.currentTimeMillis() - start);
        return flux;
    }


    private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}";

    private static final String APP_ID = "wx31cc773582e5c8b2";

    private static final String SECRET = "5c58886d820f70fc411082c74547621a";

    /**
     * 获取微信的access_token
     *
     * @return 返回结果
     */
    @RequestMapping(value = "getAccessToken", method = RequestMethod.GET)
    public Flux<Map> getAccessToken() {
        String    url = MessageFormat.format(ACCESS_TOKEN_URL, APP_ID, SECRET);
        Flux<Map> flux = WebClient.create(url).get().retrieve().bodyToFlux(Map.class);
        return flux;
    }


}
